require(["config"],function(){
	require(["jquery","template","header","footer","zoom","carousel","cookie"],function($,template){
		function Detail(){
			this.render();
			// list 传输过来的商品信息
			this.shop = {};
			this.shop_amount = 1;
		}
		Detail.prototype = {
			constructor:Detail,
			// 渲染
			render: function(){
				$.cookie.json = true;
				this.load();
			},
			// 加载数据
			load:function(){
				$.get("http://rap2api.taobao.org/app/mock/86542/shop",$.proxy(this.loadHandler,this) );
			},
			loadHandler: function(data2){
				//取得href中的id
				var id = window.location.search.slice(1).split("=")[1];
				// ajax 传过来有用的数据
				var _id = data2.res_body.data;
		
				_id.some($.proxy(function(curr,index){
					if(curr.id == id){
						// 匹配相同id的商品数据
						console.log(curr);
						var html = template("datail_template",curr);
						// 将得到的数据 传出函数					
						this.shop = curr;
						// 默认最终价格
						this.shop.lastPrice = this.shop.price;
						// 显示内容
						$(".detailContainer .more_cake").before(html);
					}
				} ,this));
				
				//addListener
				this.addListenerDetail();
			},
			//事件监听
			addListenerDetail: function(){
				console.log("listener",this.shop);
				// 轮播图
				this.carousalHandler();
				// 放大镜
				this.zoom();
				//更多精彩的轮播
				this.more();
				// + - 的使用
				$(".amount .decrease,.amount .increase").on("click",$.proxy(this.decIncHandler,this));
				//直接输入数值，改变数量多少
				$(".amount input").on("focus",$.proxy(this.focusHandler,this) )
				$(".amount input").on("blur",$.proxy(this.blurHandler,this) )
				// 点击加入购物车
				$(".add-cart").on("click",$.proxy(this.addCartHandler,this))
				// 点击立即购买
				$("#bay_cake").on("click",$.proxy(this.bayCakeHandler,this))
				// 选择 规格
				$(".cake_select .select").on("click","i",$.proxy(this.selectFocusHandler,this))
				$(".cake_select .select").on("mouseenter",function(){
					$(".cake_select .select i").css("display","block");
				})
				$(".cake_select .select").on("mouseleave",function(){
					$(".cake_select .select i").css("display","none");
					$(".cake_select .select").children(" :first ").css( "display","block" )
				})
			},
			// 调用放大镜插件
			zoom:function(){
				console.log( $(".lunbo li"));
				var lastImg= null;
				lastImg = $(".lunbo li img");
				console.log(lastImg,"lastImg");
				lastImg.elevateZoom({
					cursor: 'pointer', 
					galleryActiveClass: 'active',
					zoomWindowWidth: 485,
					zoomWindowHeight: 485,
					zoomWindowOffety: 0, 
					zoomWindowOffetx: 20,
					tintOpacity:1,
					borderSize:0
				});
//				$(".lunbo li").each(function(){
// 					if ($(this).css("display") == "block")
// 					
// 					// 赛选出当前轮播显示的图片 盒子
// 					lastImg= $(this);
// 					lastimg = lastImg.children("a").children("img");
// 				} )
// 				
			},
			// 轮播图插件
			carousalHandler: function(){
				console.log("轮播图中this = ",this);
				$(".lunbo").carousel({
					imgs : [
						{href:"#", src:this.shop.Carousal._img1},
						{href:"#", src:this.shop.Carousal._img2}
					],
					width: 485,
					height: 485,
					duration: 2000,
					color:["none","#ccc"]
				});
				 //左右键
				const _prev=$(".lunbo .prev,.lunbo .next");
				_prev.css("height","45px");
				_prev.css("opacity","0.3");
				_prev.css("margin","440px 3px 0");
				_prev.css("line-height","36px");
				//小圆点盒子
				const _page = $(".lunbo .pages");
				_page.css("background","none");
				_page.css("text-align","center");
				
				const _pagei = $(".lunbo .pages i");
				_pagei.css("margin","10px 3px");
				//添加HTML元素属性，用作放大镜需要的图片
				//console.log("tupian = ",$(".lunbo li:first-child img") );
				 $(".lunbo li:first-child img").attr("data-zoom-image",this.shop.Carousal._big1);
				 $(".lunbo li:last-child img").attr("data-zoom-image",this.shop.Carousal._big2);
			},
		
			// +- 修改数量
			decIncHandler:function(event){
				var src = event.target;
				var input = $(".amount input");
				if( $(src).is(".decrease") ){
					if( input.val() >1 ){
						input.val( input.val()-1 );
					}
				}
				if( $(src).is(".increase") ){
					input.val( parseInt(input.val())+1 );
				}
				//修改金额
				this.amendAmount();
				// 修改对象的数量
				this.shop_amount = $(".amount input").val();
			},
			
			// 直接修改数量
			focusHandler:function(){
				console.log(this);
				this.shop_amount = $(".amount input").val();
			},
			blurHandler:function(){
				console.log("input = ",this);
				var reg = /^[1-9]\d*$/;
				// 判断输入格式正确与否
				if(!reg.test( $(".amount input").val() )){
					$(".amount input").val(this.shop_amount);
				}else{
					this.shop_amount = $(".amount input").val();
				}
				this.amendAmount();
			},
			
			// 选择 规格
			selectFocusHandler:function(event){
				var event = event.target;
				var _select = $(".cake_select .select");
				_select.children(":first").text( event.innerText);
				_select.children(" i ").css( "display","none" );
				_select.children(" :first ").css( "display","block" )
				
				// 计算小计
				this.amendAmount();
			},
			
			// 修改小计金额
			amendAmount:function(){
				// 显示数据盒子
				var span = $(".desc span i");
				// 单价的选择,只考虑2个数字
				var _select = $(".cake_select .select");
				var price2 = this.shop.price;
				if( _select.children(" :first ").text() == _select.children(" :last ").text() ){
					price2 = this.shop.price_more;
					// 修改最终价格的值
					this.shop.lastPrice = price2;
				}

				span.text( price2*$(".amount input").val() );
				console.log("金额修改",price2,$(".amount input").val());
			},
			// 点击立即购买
			bayCakeHandler:function(enevt){
				// js方法不需要target
				enevt.preventDefault();
				console.log("点击立即购买");
				this.cookieAddCart();
			},
			// 点击加入购物车
			addCartHandler:function(){
				console.log("请选择产品规格",$(".cake_select .select ").children(":first") );
				if( $(".cake_select .select ").children(":first").text() == "请选择" ){
					alert("请选择产品规格");
					return ;
				}
				// 生成字符串，存入cookie
				this.cookieAddCart();
				// 显示提示界面
				$(".h_add").css("display","block");
				// 绑定关闭提示信息页面
				$(".h_add .close").on("click",function(){
					$(this).parents(".h_add").css("display","none");
				})
			},
			// 添加购物车cookie操作
			cookieAddCart:function(){
				console.log("进行了cookie操作");

				// 生成字符串，存入cookie
				var cookieArray =  $.cookie("myCart") || [];
				// 待存入数据的的商品信息
				var cookieVal = {
					// 商品id 
					id: this.shop.id,
					// 商品规格不同的价格差异
					price:this.shop.lastPrice,
					// 购买商品数量
					amount:parseInt(this.shop_amount)
				};
				// 判断 商品id
				var has = cookieArray.some(function(curr,index){
					if(curr.id == cookieVal.id){
						// 相同id的商品，数量相加就好
						curr.amount += cookieVal.amount;
						return true;
					}
					return false;
				})
				// 未选购de 商品
				if(!has){
					cookieArray.push(cookieVal);
				}
				console.log(this, cookieArray,cookieVal );
				$.cookie("myCart",cookieArray,{path:"/"});
			},
			
			// 更多精彩 具体
			more:function(){
				var wrapMore = $(".wrap-more");
				// 设置图片轮播的定时器
				console.log("定时器启动");
				var moreInter = setInterval(function(){
					if( parseInt(wrapMore.css("left")) <0 ){
						wrapMore.css("left","0");
					}else{
						wrapMore.css("left","-333px");
					}
				},3000);
				// 设置鼠标移入，移出事件
				wrapMore.on("mouseenter",function(){
					console.log("定时器暂停",moreInter)
					clearInterval(moreInter);
				})
				wrapMore.on("mouseleave",function(){
					console.log("定时器启动");
					moreInter = setInterval(function(){
						if( parseInt(wrapMore.css("left")) <0 ){
							wrapMore.css("left","0");
						}else{
							wrapMore.css("left","-333px");
						}
					},3000);
				} )//on
			}
		}
		
		
		new Detail();
	})
})